package com.zhiyou100.dao.Impl;

import com.zhiyou100.JDBCUtil.DBUtil;
import com.zhiyou100.dao.LogisticsDao;
import com.zhiyou100.emtity.Logistics;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.BeanUtils;

import javax.lang.model.util.SimpleAnnotationValueVisitor6;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-21 12:58
 **/
public class LogisticsDaoImpl implements LogisticsDao {

    private QueryRunner qur = new QueryRunner(DBUtil.getDataSource());

    @Override
    public List<Logistics> queryAll() {
        String sql = "select * from logistics";
        try {
            List<Logistics> list = qur.query(sql, new BeanListHandler<>(Logistics.class));
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void add(Logistics logistics) {
        String sql = "insert into logistics values(null,?,?,?,?,?,?,?,?)";
        try {
            qur.update(sql, logistics.getL_name(), logistics.getL_idNumber(), logistics.getL_phone(), logistics.getL_sex(), logistics.getL_post(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), logistics.getL_pay(), logistics.getL_nativePlace());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }

    @Override
    public void delete(int id) {
        String sql = "delete from logistics where l_id = ?";
        try {
            qur.update(sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public Logistics query(int id) {
        String sql = "select * from logistics where l_id = ?";
        try {
            Logistics query = qur.query(sql, new BeanHandler<>(Logistics.class), id);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public void update(Logistics logistics) {
        String sql = "update logistics set l_name=?,l_idNumber=?,l_phone=?,l_sex=?,l_post=?,l_addTime=?,l_pay=?,l_nativePlace=? where l_id=?";
        try {
            qur.update(sql, logistics.getL_name(),logistics.getL_idNumber(),logistics.getL_phone(),logistics.getL_sex()
                        ,logistics.getL_post(),logistics.getL_addTime(),logistics.getL_pay(),logistics.getL_nativePlace(),logistics.getL_id() );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return;
    }
}
